package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.CourseDescription;
import com.atguigu.guli.service.edu.entity.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.search.CourseInfoSearch;
import com.atguigu.guli.service.edu.entity.vo.CourseDetailVo;
import com.atguigu.guli.service.edu.service.CourseDescriptionService;
import com.atguigu.guli.service.edu.service.CourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author leo
 * @since 2021-11-24
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/admin/edu/course")

public class AdminCourseController {
    @Autowired
    private CourseService courseService ;
    @Autowired
    private CourseDescriptionService courseDescriptionService ;
    @ApiOperation("新增课程")
    @PostMapping(value = "save-course-info")
    public R saveCourseInfo(@ApiParam(value = "课程基本信息",required = true) @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm) ;
        return R.ok().data("courseId",courseId).message("新增课程成功") ;
    }

    @ApiOperation("查询课程")
    @GetMapping("get-course-info/{courseId}")
    public R getCourseInfo(@ApiParam(value = "课程Id",required = true) @PathVariable String courseId) {
        CourseInfoForm courseInfoForm = new CourseInfoForm() ;
        Course course = courseService.getById(courseId);
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);
        BeanUtils.copyProperties(course,courseInfoForm);
        courseInfoForm.setDescription(courseDescription.getDescription());
        return R.ok().data("item",courseInfoForm) ;
    }

    @ApiOperation("更新课程")
    @PutMapping("update-course-info/{courseId}")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm,@PathVariable String courseId) {
        courseService.updateCourseInfo(courseInfoForm,courseId) ;
        return R.ok() ;
    }

    @ApiOperation("带条件的分页查询")
    @GetMapping("list/{pageNum}/{pageSize}")
    public R listPage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, CourseInfoSearch courseInfoSearch) {
        Page<CourseDetailVo> page = new Page<>(pageNum,pageSize) ;
        courseService.getInfoSearch(page, courseInfoSearch) ;
        return R.ok().data("page",page) ;
    }
}

